<template>
	<view>
		<view class="shop_t">
			<view @click="change(1)" :class="typechang == 1?'ats':'ts'">
				可使用
			</view>
			<view @click="change(2)" :class="typechang == 2?'ats':'ts'">
				不可用卡
			</view>
		</view>
		<view class="its" v-for="(item,index) in list" :key="index" @click="pagedetail(item)">
			<image v-if="typechang == 1" src="../static/user/lp.png" mode=""></image>
			<image v-else src="../static/user/lph.png" mode=""></image>
			<view class="abs">
				<view class="abs_tit">
					<view class="flex_b">
						<view class="yuan" :style="{background:item.color}">
							<image src="../static/icon/lp.png" mode=""></image>
						</view>
						<view class="uname">
							{{item.cardName}}
						</view>
					</view>
					<view class="time" :style="{color:typechang == 1?'#7E808C':'#ffffff'}">
						有效期至：{{item.endTime.substring(0,10)}}
					</view>
				</view>
				<view class="price" :style="{color:typechang == 1?'#29A85C':'#ffffff'}">
					<span class="s1" :style="{color:typechang == 1?'#29A85C':'#ffffff'}">￥</span>{{item.amountBalance}}
					<span class="s2">元</span>
				</view>

				<view class="num">
					{{item.cardNumber.replace(/(\d{4})(?=\d)/g, '$1 ')}}
				</view>
			</view>
		</view>
		<view class="kong" v-if="list.length == 0">
			<view class="">
				<image src="../static/lpk.png" mode=""></image>
				<view class="ts">
					尚无礼品卡
				</view>
			</view>
		</view>
		<view class="" style="height: 300rpx;"></view>
		<view class="footerbtn" @click="page">
			<view class="cont">
				<view class="btn">
					绑定礼品卡
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		cardYes,
		cardNo
	} from '@/common/http.api.js';
	export default {
		data() {
			return {
				typechang: 1,
				colorList: [
					'#C30000',
					'#EB5A00',
					'#29A85C',
					'#FFAF43',
					'#000000',
				],
				params: {
					pageNum: 1,
					pageSize: 10
				},
				nolist: false,
				list: []
			};
		},
		onShow() {
			this.params.pageNum = 1
			this.list = []
			if(this.typechang == 1){
				cardYes(this.params).then(res => {
					res.data.forEach(item => {
						item.color = this.colorList[Math.floor(Math.random() * 5)]
					})
					this.list = res.data
				})
			}else{
				cardNo(this.params).then(res=>{
					res.data.forEach(item => {
						item.color = this.colorList[Math.floor(Math.random() * 5)]
					})
					this.list = res.data
				})
			}
			
		},
		methods: {
			pagedetail(item) {
				uni.navigateTo({
					url:`/mypage/giftdetail?item=`+ encodeURIComponent(JSON.stringify(item))
				})
			},
			page() {
				uni.navigateTo({
					url: '/mypage/dhlpk'
				})
			},
			change(type) {
				this.typechang = type
				this.params.pageNum = 1
				if(type == 1){
					cardYes(this.params).then(res=>{
						res.data.forEach(item => {
							item.color = this.colorList[Math.floor(Math.random() * 5)]
						})
						this.list = res.data
						let total = Math.ceil(res.total / this.params.pageSize);
						this.nolist = total == 0
					})
				}else{
					cardNo(this.params).then(res=>{
						res.data.forEach(item => {
							item.color = this.colorList[Math.floor(Math.random() * 5)]
						})
						this.list = res.data
						let total = Math.ceil(res.total / this.params.pageSize);
						this.nolist = total == 0
					})
				}
			}
		}
	}
</script>

<style lang="scss">
	page {
		background: #FBFBFB;
	}

	.kong {
		width: 100%;
		margin: 230rpx auto;
		display: flex;
		align-items: center;
		justify-content: center;
		text-align: center;

		image {
			width: 228rpx;
			height: 228rpx;
		}

		.ts {
			font-family: PingFang SC, PingFang SC;
			font-weight: bold;
			font-size: 36rpx;
			color: #000000;
			margin-top: 20rpx;
		}
	}

	.shop_t {
		display: flex;
		background: #fff;
		padding-top: 10rpx;
		position: sticky;
		top: 0%;
		z-index: 9;

		.ts {
			width: 50%;
			text-align: center;
			font-family: PingFang SC, PingFang SC;
			font-weight: 500;
			font-size: 28rpx;
			color: #979797;
			padding-bottom: 20rpx;
			border-bottom: 2px solid #E6E6E6;
		}

		.ats {
			width: 50%;
			text-align: center;
			font-family: PingFang SC, PingFang SC;
			font-weight: bold;
			font-size: 32rpx;
			color: #000000;
			padding-bottom: 20rpx;
			border-bottom: 2px solid #000000;
		}
	}

	.its {
		width: 710rpx;
		height: 260rpx;
		margin: 20rpx auto;
		position: relative;

		image {
			width: 100%;
			height: 260rpx;
		}

		.abs {
			top: 0%;
			position: absolute;
			width: 100%;
			height: 260rpx;
			padding: 26rpx 20rpx;
			box-sizing: border-box;

			.abs_tit {
				display: flex;
				align-items: center;
				justify-content: space-between;
			}

			.yuan {
				width: 40rpx;
				height: 40rpx;
				border-radius: 20rpx;

				image {
					width: 40rpx;
					height: 40rpx;
				}
			}

			.uname {
				font-family: PingFang SC, PingFang SC;
				font-weight: bold;
				font-size: 32rpx;
				color: #FFFFFF;
				margin-left: 20rpx;
			}

			.time {
				font-family: PingFang SC, PingFang SC;
				font-weight: 500;
				font-size: 20rpx;
				color: #7E808C;
			}

			.price {
				font-family: 'DINB';
				font-size: 72rpx;
				margin-top: 44rpx;
				color: #29A85C;

				.s1 {
					font-size: 40rpx;
				}

				.s2 {
					font-size: 32rpx;
					color: #FFFFFF;
				}
			}

			.num {
				font-family: PingFang SC, PingFang SC;
				font-weight: 500;
				font-size: 24rpx;
				color: #FFFFFF;
				margin-top: 10rpx;
			}
		}

	}

	.footerbtn {
		width: 750rpx;
		height: 120rpx;
		background: #FFFFFF;
		position: fixed;
		bottom: 0%;
		padding-bottom: constant(safe-area-inset-bottom);
		padding-bottom: env(safe-area-inset-bottom);

		.cont {
			display: flex;
			padding: 10rpx 20rpx;
			height: 120rpx;
			font-family: PingFang SC, PingFang SC;
			font-weight: 500;
			font-size: 32rpx;
			align-items: center;
			justify-content: center;

			.btn {
				width: 670rpx;
				height: 100rpx;
				background: #000000;
				border-radius: 8rpx 8rpx 8rpx 8rpx;
				color: #FFFFFF;
				font-family: PingFang SC, PingFang SC;
				font-weight: 500;
				font-size: 32rpx;
				color: #FFFFFF;
				line-height: 100rpx;
				text-align: center;
			}

			.absbtn {
				background: #808080;
			}
		}
	}
</style>